package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.UserPojoMapper;
import com.qf.mapper.UserRolePojoMapper;
import com.qf.pojo.UserPojo;
import com.qf.pojo.UserRolePojo;
import com.qf.service.UserPojoService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.ref.WeakReference;
import java.util.List;

@Service
public class UserPojoServiceImpl extends ServiceImpl<UserPojoMapper,UserPojo> implements UserPojoService {

    @Autowired
    UserPojoMapper userPojoMapper;

    @Autowired
    UserRolePojoMapper userRolePojoMapper;

    @Override
    @Transactional
    public ResultMsg add(UserPojo userPojo) {
        boolean save = this.save(userPojo);
        if (save){
            UserRolePojo userRolePojo = new UserRolePojo();
            userRolePojo.setUserId(userPojo.getUserId());
            userRolePojo.setRoleId(userPojo.getRoleId());
            int insert = userRolePojoMapper.insert(userRolePojo);
            if (insert==1)
                return ResultMsg.SUCCESS("添加成功");
        }
        return ResultMsg.FAILED(Constant.WB,"添加失败");
    }

    @Override
    @Transactional
    public ResultMsg updatePass(Integer userId, String userPass) {

            int i = userPojoMapper.changepass(userId, userPass);
            if (i==1){
                return ResultMsg.SUCCESS("密码修改成功");
            }
            return ResultMsg.FAILED(Constant.WB,"密码修改失败");

    }

    @Override
    @Transactional
    public ResultMsg delById(Integer userId) {
        boolean b = this.removeById(userId);
        if (b){
            return ResultMsg.SUCCESS("删除成功");
        }
        return ResultMsg.FAILED(Constant.WB,"删除失败");
    }

    @Override
    public ResultMsg findAll(String keyword) {
        QueryWrapper<UserPojo> wrapper = new QueryWrapper<>();
        wrapper.like("user_name", keyword);
        wrapper.or();
        wrapper.like("user_tel",keyword);
        List<UserPojo> list = this.list(wrapper);
        if (list!=null)
            return ResultMsg.SUCCESS(list);
        return ResultMsg.FAILED(Constant.WB,"没有这个姓名");
    }

    @Override
    @Transactional
    public ResultMsg updateInfo(UserPojo userPojo) {
        UpdateWrapper<UserPojo> wrapper = new UpdateWrapper<>();
        UserRolePojo userRolePojo = new UserRolePojo();
        boolean update = this.updateById(userPojo);
        if (update){
            UpdateWrapper<UserRolePojo> wrapper1 = new UpdateWrapper<>();
            wrapper1.eq("user_id",userPojo.getUserId());
            userRolePojo.setUserId(userPojo.getUserId());
            userRolePojo.setRoleId(userPojo.getRoleId());
            int i = userRolePojoMapper.delete(wrapper1);
            if (i==1){
                int insert = userRolePojoMapper.insert(userRolePojo);
                if (insert==1){
                    return ResultMsg.SUCCESS("修改成功");
                }

            }
        }
        return ResultMsg.FAILED(Constant.WB,"修改失败");
    }
}
